﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BabyLearnEnglish_srcum_demo
{
    public class HighScore
    {
        public string Name;
        public int Score;

        public List<HighScore> getList()
        {
            List<HighScore> list = new List<HighScore>();
            string line;

            System.IO.StreamReader file = new System.IO.StreamReader("../../data/highscore.txt");
            while ((line = file.ReadLine()) != null)
            {
                HighScore h = new HighScore();
                h.Name = line.Split('|')[0];
                h.Score = int.Parse(line.Split('|')[1]);
                list.Add(h);
            }
            file.Close();
            return list;
        }

        public void saveList(List<HighScore> _list)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter("../../data/highscore.txt");
            foreach (HighScore h in _list)
            {
                string line = h.Name + '|' + h.Score;
                file.WriteLine(line);
            }
            file.Close();
        }
        
        public int orderAndSaveList(HighScore h)
        {
            List<HighScore> list = getList();
            for (int i = 0; i < list.Count; i++)
            {
                if (h.Score > list[i].Score)
                {
                    list.Insert(i, h);
                    saveList(list);
                    return i;
                }
            }
            list.Add(h);
            saveList(list);
            return list.Count-1;
        }
    }
}
